package cn.francis.service.impl;

import cn.francis.service.CompanyService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.agro.mapper.CompanyMapper;
import com.qfedu.agro.pojo.Company;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Name: CompanyServiceImpl
 * Package: cn.francis.service.impl
 * Date: 2024/10/26 - 17:31
 * Description:
 *
 * @author Junhui Zhang
 * @version v1.0
 */

@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public Page<Company> findPage(Integer pageNum, Integer pageSize, String name) {
        LambdaQueryWrapper<Company> companyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        companyLambdaQueryWrapper.like(StringUtils.isNotEmpty(name), Company::getName, name);
        companyLambdaQueryWrapper.eq(Company::getState, 1);
        companyLambdaQueryWrapper.orderByDesc(Company::getId);
        Page<Company> companyPage = new Page<>(pageNum, pageSize);
        companyMapper.selectPage(companyPage, companyLambdaQueryWrapper);
        return companyPage;
    }

    @Override
    public void delete(Long id) {
        Company company = new Company();
        company.setId(id);
        company.setState(0);
        try {
            companyMapper.updateById(company);
        } catch (Exception e) {
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    public void insert(Company company) {
        try {
            companyMapper.insert(company);
        } catch (Exception e) {
            throw new RuntimeException("新增失败");
        }
    }
}
